DRY vs WET
DRY (Don’t Repeat Yourself)
DRY is a principle that encourages avoiding duplication of code or logic. If something is repeated in multiple places, it should be abstracted into a single place (function, module, class, etc.).
- Reduce redundancy
- Make maintenance easier
- Avoid bugs that happen due to inconsistent updates
// NOT DRY
console.log("Hello, Alice!");
console.log("Hello, Bob!");
console.log("Hello, Charlie!");
// DRY version
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Alice");
greet("Bob");
greet("Charlie");
Now if we want to change the greeting, we only change it in one place.
WET (Write Everything Twice / We Enjoy Typing)
WET is the opposite of DRY. It’s when code is duplicated instead of abstracted. Sometimes people use WET intentionally for simplicity, or because over-abstraction can make code harder to read.
console.log("Hello, Alice!");
console.log("Hello, Bob!");
console.log("Hello, Charlie!");
If we want to change "Hello" to "Hi", we must update every line manually, which is error-prone.
Comparison of DRY and WET
| Aspect | DRY | WET |
|---|---|---|
| Approach | Reuse code / abstract logic | Duplicate code |
| Maintenance | Easy, changes in one place | Hard, multiple changes needed |
| Risk of Bugs | Low | High |
| Example | Functions, classes, modules | Repeated print statements, copied logic |